package com.sntk.organauthomanager.application.controller;

import com.alibaba.fastjson.JSON;
import com.sntk.organauthomanager.application.dto.*;
import com.sntk.organauthomanager.application.service.ResourceApplicationService;
import com.sntk.organauthomanager.infrastructure.session.LoginSessionInfo;
import com.sntk.organauthomanager.infrastructure.validation.Insert;
import com.sntk.organauthomanager.infrastructure.validation.Update;
import com.sntk.organauthomanager.infrastructure.validation.Validation;
import com.sntk.organauthomanager.query.ResourceQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @ClassName: ResourceController
 * @author: wgchao
 * @createTime: 2020/7/21 5:54 PM
 * @Description: TODO
 */
@RestController
@RequestMapping("/sinohis/osm/external/sys/resource")
@Api(value="ResourceController",tags={"资源信息管理"})
public class ResourceController {
    @Autowired
    private ResourceApplicationService resourceApplicationService;
    @Autowired
    private ResourceQuery resourceQuery;

    @PostMapping
    @ApiOperation(value="添加资源信息")
    public ResponseData createResource(@ApiParam(name="creator",value="资源信息",required=true)
                                       @RequestBody @Validated(value = {Insert.class}) ResourceCreator creator, BindingResult result, HttpSession httpSession){
        List<?> l = Validation.getValidation(result);
        if(l.size() != 0) {
            return ResponseData.error(JSON.toJSONString(l));
        }
        resourceApplicationService.createResource(creator, LoginSessionInfo.generate(httpSession).getOperator());
        return ResponseData.success();
    }

    @PutMapping
    @ApiOperation(value="修改资源信息")
    public ResponseData modifyResourceInfo(@ApiParam(name="resourceDTO",value="资源信息",required=true)
                                       @RequestBody @Validated(value = {Update.class}) ResourceDTO resourceDTO, BindingResult result, HttpSession httpSession){
        List<?> l = Validation.getValidation(result);
        if(l.size() != 0) {
            return ResponseData.error(JSON.toJSONString(l));
        }
        resourceApplicationService.modifyResourceInfo(resourceDTO, LoginSessionInfo.generate(httpSession).getOperator());
        return ResponseData.success();
    }

    @DeleteMapping("/{mark}")
    @ApiOperation(value="删除资源")
    public ResponseData removeResource(@ApiParam(name="mark",value="资源标识",required=true)
                                   @PathVariable String mark, HttpSession httpSession){
        resourceApplicationService.removeResource(mark, LoginSessionInfo.generate(httpSession).getOperator());
        return ResponseData.success();
    }

    @GetMapping("/top/tree")
    @ApiOperation(value="查询资源树")
    public ResponseData<List<ResourceVO>> searchRolesOfCondition(){
        List<ResourceVO> resourceVOS = resourceQuery.searchResourcesWithTree();
        return ResponseData.success().setData(resourceVOS);
    }

    @GetMapping("/{mark}")
    @ApiOperation(value="查询资源信息")
    public ResponseData<ResourceDTO> searchResource(@ApiParam(name="mark",value="资源标识",required=true)
                                                @PathVariable String mark){
        ResourceDTO resourceDTO = resourceQuery.searchResource(mark);
        return ResponseData.success().setData(resourceDTO);
    }

    @GetMapping("/top/tree/{promark}")
    @ApiOperation(value="查询项目资源树")
    public ResponseData<ResourceVO> searchResourcesOfProWithTree(@ApiParam(name="promark",value="项目资源标识",required=true)
                                                                     @PathVariable String promark){
        ResourceVO resourceVO = resourceQuery.searchResourcesOfProWithTree(promark);
        return ResponseData.success().setData(resourceVO);
    }

    @GetMapping("/list/role/{roleCode}/{institutionNumber}")
    @ApiOperation(value="查询角色可用资源")
    public ResponseData<List<String>> searchResourcesOfRole(@ApiParam(name="roleCode",value="角色码",required=true)
                                                                @PathVariable String roleCode,
                                                            @ApiParam(name="institutionNumber",value="机构编码",required=true)
                                                                @PathVariable String institutionNumber){
        List<String> resourceVOs = resourceQuery.searchResourcesOfRole(roleCode, institutionNumber, false);
        return ResponseData.success().setData(resourceVOs);
    }

    @GetMapping("/list/loginUser")
    @ApiOperation(value="查询登陆用户可用资源")
    public ResponseData<List<String>> searchResourcesOfUser(HttpSession httpSession){
        OperatorInfo operatorInfo = LoginSessionInfo.generate(httpSession).getOperatorInfo();
        List<String> resourceVOs = resourceQuery.searchResourcesOfUser(operatorInfo);
        return ResponseData.success().setData(resourceVOs);
    }

    @GetMapping("/list/user/{uname}/{institutionNumber}")
    @ApiOperation(value="查询用户私有资源")
    public ResponseData<List<String>> searchResourcesOfUserOwn(@ApiParam(name="uname",value="账户名",required=true)
                                                                @PathVariable String uname,
                                                            @ApiParam(name="institutionNumber",value="机构编码",required=true)
                                                                @PathVariable String institutionNumber){
        List<String> resourceVOs = resourceQuery.searchResourcesOfUserOwn(uname, institutionNumber, false);
        return ResponseData.success().setData(resourceVOs);
    }
}
